package com.dingli.service.impl;

import com.dingli.framework.entity.Undertake;
import com.dingli.framework.mapper.UndertakeMapper;
import com.dingli.framework.util.Response;
import com.dingli.service.IUndertakeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UndertakeService implements IUndertakeService {

    @Autowired
    private UndertakeMapper undertakeMapper;

    @Override
    public Response<PageInfo<Undertake>> selectList(String search, Integer pageNum, Integer pageSize) {
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<Undertake> info = new PageInfo<>(undertakeMapper.selectList(search));
        return new Response<>(200, "success", info);
    }

    @Override
    public Response<List<Undertake>> selectAll(Long ukeId) {
        return new Response<>(200, "success", undertakeMapper.selectAll(ukeId));
    }

    @Override
    public Response<Object> updateState(String ukeId) {
        return new Response<>(200, "success", undertakeMapper.updateState(ukeId));
    }

    @Override
    public Response<Object> update(String ukeId) {
        return new Response<>(200, "success", undertakeMapper.update(ukeId));
    }

    @Override
    public Response<List<Undertake>> select(String ukeId) {
        return new Response<>(200, "success", undertakeMapper.select(ukeId));
    }

    @Override
    public Response<List<Undertake>> selectName(String typeName) {
        return new Response<>(200, "success", undertakeMapper.selectName(typeName));
    }

    @Override
    public Response<Long> insert(Undertake undertake) {
        int count = undertakeMapper.insert(undertake);
        if (count > 0) {
            return new Response(200, "success", undertake.getUkeId());
        } else {
            return new Response<>(500, "failed");
        }
    }

    @Override
    public Response<Object> updateById(Undertake undertake) {
        int count = undertakeMapper.updateById(undertake);
        if (count > 0) {
            return new Response<>(200, "success");
        } else {
            return new Response<>(500, "failed");
        }
    }

    @Override
    public Response<Object> deleteById(Long ukeId) {
        int count = undertakeMapper.deleteById(ukeId);
        if (count > 0) {
            return new Response<>(200, "success");
        } else {
            return new Response<>(500, "failed");
        }
    }
}